<?php

namespace App\Models;

use App\Common\Paginate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 房源接口
 */
class TestHousing extends Model
{
    use HasFactory;
    //黑名单
    protected $guarded = [];
    //白名单
    protected $fillable = ['uid','agent_id','logo','location','is_agent','estate_id','region','floor','total_floor','area','price','title','content','person'];
    //指定表
    protected $table = "fang_publish";

    //多条件搜索分页
    public static function getHouse($params, $limit, $offset)
    {
        //
        $data['data']= self::when($params, function ($query) use ($params) {
            if (isset($params['location']) && !empty($params['location'])) {
                $query->where('location', 'like', '%' . $params['location'] . '%');
            }
            if (isset($params['price']) && !empty($params['price'])) {
                $query->Where('price', '>',$params['price']);
            }
        })->offset($offset)->limit($limit)->get();//offset是偏移量; //mit展示每页条数


        $data['total'] =(int)self::when($params, function ($query) use ($params) {
            if (isset($params['location']) && !empty($params['location'])) {
                $query->where('location', 'like', '%' . $params['location'] . '%');
            }
            if (isset($params['price']) && !empty($params['price'])) {
                $query->Where('price', '>',$params['price']);
            }
        })->count();//offset是偏移量; //mit展示每页条数;

        $data['limit'] =(int)$limit;
        return $data;
    }

    //添加房源
    static function createHouse(array $param)
    {
        return self::create($param);
    }

    //详情
    static function detailHouse($where)
    {
        return self::where($where)->first();
    }

    //修改房源&删除房源
    static function updateHouse($where, $update)
    {
        return self::where($where)->update($update);
    }


}
